# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms_dm_duty.dm.dm_whole_decide_duty_static_dt import jms_dm__dm_whole_decide_duty_static_dt

label = f"dm_whole_decide_duty_static_dt_{int(time())}"
timeout = timedelta(minutes=60).seconds
doris_jms_dm__dm_whole_decide_duty_static_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_whole_decide_duty_static_dt',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris_uat',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_whole_decide_duty_static_dt PARTITION (
         p{{{{ execution_date | date_add(-5) | cst_ds_nodash }}}} 
        ,p{{{{ execution_date | date_add(-4) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-3) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+0) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+3) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+4) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+5) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(-5) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(-4) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(-3) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+0) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+1) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+2) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+3) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+4) | cst_ds }}}}/*",
            "hdfs://{{{{var.value.hadoop_namespace_uat}}}}/dw/hive/jms_tmp.db/external/dm_whole_decide_duty_static_dt/dt={{{{ execution_date | date_add(+5) | cst_ds }}}}/*"
        )
        INTO TABLE dm_whole_decide_duty_static_dt
        FORMAT AS 'PARQUET'(
             waybill_no
            ,first_network_code
            ,first_network_name
            ,sign_network_code
            ,sign_network_name
            ,last_network_code
            ,last_network_name
            ,scan_site_plan_no
            ,scan_site_plan_code
            ,scan_site_plan_name
            ,scan_site_plan_type
            ,plan_last_taking_time
            ,scan_site_plan_arrival_time
            ,scan_site_plan_send_time
            ,next_site_plan_code
            ,next_site_plan_name
            ,next_site_plan_type
            ,plan_sign_time
            ,plan_deliver_time
            ,total_days
            ,plan_diff_time
            ,plan_node
            ,route_id
            ,cainiao_plan_sign_time
            ,ziyou_plan_sign_time
            ,scan_site_plan_last_arrival_time
        )
        columns from path as (dt)
        set(
             waybill_no                        = waybill_no
            ,first_network_code                = first_network_code
            ,first_network_name                = first_network_name
            ,sign_network_code                 = sign_network_code
            ,sign_network_name                 = sign_network_name
            ,last_network_code                 = last_network_code
            ,last_network_name                 = last_network_name
            ,scan_site_plan_no                 = scan_site_plan_no
            ,scan_site_plan_code               = scan_site_plan_code
            ,scan_site_plan_name               = scan_site_plan_name
            ,scan_site_plan_type               = scan_site_plan_type
            ,plan_last_taking_time             = plan_last_taking_time
            ,scan_site_plan_arrival_time       = scan_site_plan_arrival_time
            ,scan_site_plan_send_time          = scan_site_plan_send_time
            ,next_site_plan_code               = next_site_plan_code
            ,next_site_plan_name               = next_site_plan_name
            ,next_site_plan_type               = next_site_plan_type
            ,plan_sign_time                    = plan_sign_time
            ,plan_deliver_time                 = plan_deliver_time
            ,total_days                        = total_days
            ,plan_diff_time                    = plan_diff_time
            ,plan_node                         = plan_node
            ,route_id                          = route_id
            ,cainiao_plan_sign_time            = cainiao_plan_sign_time
            ,ziyou_plan_sign_time              = ziyou_plan_sign_time
            ,scan_site_plan_last_arrival_time  = scan_site_plan_last_arrival_time
            ,dt                                = dt
        )
    )
    WITH BROKER '{{{{ var.json.doris_brokers_uat | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )
doris_jms_dm__dm_whole_decide_duty_static_dt << jms_dm__dm_whole_decide_duty_static_dt
